package cn.edu.nks.dormitory.student.editinfo.dbimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;


import org.apache.log4j.Logger;

import cn.edu.nks.dormitory.model.StudentInfo;
import cn.edu.nks.dormitory.student.editinfo.db.IDBService;
import cn.edu.nks.util.DBConnectProvider;
import cn.edu.nks.util.DateTimeUtil;

public class DBService implements IDBService{
	
	private static Logger logger = Logger.getLogger(DBService.class);
	
	public StudentInfo getStudentInfoByUserId(int userId) {
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		StudentInfo item = null;
		
		con = DBConnectProvider.getConnection();
		if(null == con){
			logger.warn("get connect faild");
		}
		StringBuffer sql = new StringBuffer();
		sql.append("select * from studentinfo where userid = ?");
		try {
			stmt = con.prepareStatement(sql.toString());
			stmt.setInt(1, userId);
			rs = stmt.executeQuery();
			if(rs.next()){
				item = new StudentInfo();
				item.setAddr(rs.getString("addr"));
				item.setBirthday(DateTimeUtil.dateNow());
				item.setClassId(rs.getInt("classId"));
				item.setGrade(rs.getInt("grade"));
				item.setEmail(rs.getString("email"));
				item.setHeader(rs.getString("header"));
				item.setHeight(rs.getInt("height"));
				item.setIncomedate(DateTimeUtil.dateNow());
				item.setJsons(rs.getString("jsons"));
				item.setRealName(rs.getString("realname"));
				item.setRegion(rs.getString("region"));
				item.setSex(rs.getString("sex"));
				item.setStudentCode(rs.getString("studentcode"));
				item.setStudentId(rs.getInt("studentid"));
				item.setTelephone(rs.getString("telephone"));
				item.setTime(DateTimeUtil.dateNow());
				item.setUserId(rs.getInt("userid"));
				item.setWeight(rs.getDouble("weight"));
			}
		} catch (SQLException e) {
			logger.warn(String.format("getStudentInfoByUserId is error : %s", e));
		}
		return item;
	}

	public boolean updateStudentInfo(StudentInfo info) {
		Connection con = null;
		PreparedStatement stmt = null;
		boolean flag = false;
		
		con = DBConnectProvider.getConnection();
		if(null == con){
			logger.warn("get connect faild");
			return false;
		}
		StringBuffer sql = new StringBuffer();
		sql.append("update studentinfo set classid = ?,grade=?,realname = ?,telephone = ?,incomedate = ?,header = ?,");
		sql.append(" email = ?,sex = ?,birthday=?,height = ?,weight=?,region = ?,addr = ?,jsons = ?,time = ?");
		sql.append(" where studentid = ?");
		try {
			stmt = con.prepareStatement(sql.toString());
			stmt.setInt(1, info.getClassId());
			stmt.setInt(2, info.getGrade());
			stmt.setString(3, info.getRealName());
			stmt.setString(4, info.getTelephone());
			stmt.setString(5,DateTimeUtil.toDateTimeString(info.getIncomedate()));
			stmt.setString(6, info.getHeader());
			stmt.setString(7, info.getEmail());
			stmt.setString(8, info.getSex());
			stmt.setString(9, DateTimeUtil.toDateString(info.getBirthday()));
			stmt.setInt(10, info.getHeight());
			stmt.setDouble(11, info.getWeight());
			stmt.setString(12, info.getRegion());
			stmt.setString(13, info.getAddr());
			stmt.setString(14, info.getJsons());
			stmt.setString(15, DateTimeUtil.toDateTimeString(new Date()));
			stmt.setInt(16, info.getStudentId());
			flag = stmt.executeUpdate() > 0;
		} catch (SQLException e) {
			logger.warn(String.format("updateStudentInfo is error :%s", e));
		}
		DBConnectProvider.freeConnection(con);
		return flag;
	}

}
